// stations.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hots: ['郑州中心站', '高铁枢纽站', '新郑汽车站', '郑州陇海站'],
    stations: "",
    inputShowed: false,
    inputVal: ""
  },

  code: function (e) {
    var index = e.currentTarget.dataset.code;
    var name = e.currentTarget.dataset.name;
    var stationName = this.data.stations;
    if(stationName != "" && stationName != null && typeof(stationName) != 'undefined'){

    for (var i = 0; i < stationName.length; i++) {
      if (stationName[i].CityCode == index && stationName[i].CityName == name) {
        stationName[i].show = stationName[i].show == true ? false : true;
        for (var k = 0; k < stationName[i].StationList.length; k++) {
          stationName[i].StationList[k].show = stationName[i].StationList[k].show == true ? false : true;
        }
      } else {
        stationName[i].show = false;
        for (var k = 0; k < stationName[i].StationList.length; k++) {
          stationName[i].StationList[k].show = false;
        }
      }
    }
  }
    this.setData({
      stations: stationName,
    })
  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },

  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    var val = e.detail.value.toUpperCase(),
      stations = this.data.stations,
      cityRearch = [];
    if (val.length > 0) {
      for (var item in stations) {
        stations[item].show = true;
        for (var city in stations[item].StationList) {
          if (stations[item].StationList[city].StationName.indexOf(val) == 0 || stations[item].StationList[city].StationPYCode.indexOf(val) == 0) {
            stations[item].StationList[city].show = true;
            cityRearch.push(stations[item].StationList[city]);
          } else {
            stations[item].StationList[city].show = false
          }
        }
      }
      this.setData({
        cityResult: true,
        cityRearch: cityRearch
      })

    } else {
      this.setData({
        cityResult: false
      })
    }

    this.setData({
      stations: stations
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    try {
      this.StationList();
    } catch (e) { }
  },

  StationList: function () {
    app.showLoadings();
    let getToken = app.accesstoken ? app.accesstoken : app.stanToken;
    var that = this;
    wx.request({
      url: app.serviceurl + 'StationLine/QueryStations',
      header: {
        Authorization: "Bearer " + getToken
      },
      success: function (res) {
        if (res.data.CityList == null || typeof (res.data.CityList) == 'undefined') {
          that.setData({
            result: false
          });
          return;
        }
        for (var item in res.data.CityList) {
          for (var city in res.data.CityList[item].StationList) {
            res.data.CityList[item].StationList[city].show = false;
            res.data.CityList[item].StationList[city].hidden = true;
          }
        }
        res.data.CityList[0].show = true;
        for (var i in res.data.CityList[0].StationList) {
          res.data.CityList[0].StationList[i].show = true;
        }
        that.setData({
          stations: res.data.CityList,
          HotStations: res.data.HotStations,
          result: true,
        });
      },
      fail: function (xhr) {
        that.setData({
          result: false
        });
      },
      complete: function () {
        app.hideLoadings();
        that.setData({
          placeholder: '中文/简拼'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },
  setdatas: function (data) {
    this.setData({
      stations: data.CityList
    });
  },

  // 搜索
  hideInput: function () {
    this.setData({
      inputVal: "",

    });
  },

  HotStations: function (event) {
    try {
      app.globalcode = event.currentTarget.dataset.globalcode;
      app.stationName = event.currentTarget.dataset.stationname;
      wx.setStorage({
        key: 'stationname',
        data: event.currentTarget.dataset.stationname,
      });
      wx.setStorage({
        key: 'globalcode',
        data: event.currentTarget.dataset.globalcode,
      });
      wx.navigateBack({});
    } catch (err) { }
  },

  selectstation: function (event) {
    try {
      app.globalcode = event.currentTarget.dataset.globalcode;
      app.stationName = event.currentTarget.dataset.stationname;
      // let stationInfo = {
      //   stationname: event.currentTarget.dataset.stationname,
      //   globalcode: event.currentTarget.dataset.globalcode
      // };
      // wx.setStorage({
      //   key: 'stationInfo',
      //   data: stationInfo,
      // })
      
      wx.setStorage({
        key: 'stationname',
        data: event.currentTarget.dataset.stationname,
      });
      wx.setStorage({
        key: 'globalcode',
        data: event.currentTarget.dataset.globalcode,
      });
      wx.navigateBack({});
    } catch (err) { }
  },

})